package weiy.top.modules.spu.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import weiy.top.modules.attr.entity.PmsAttr;
import weiy.top.modules.common.feign.CouponFeignService;
import weiy.top.modules.common.vo.*;
import weiy.top.modules.product.entity.PmsProductAttrValue;
import weiy.top.modules.product.service.IPmsProductAttrValueService;
import weiy.top.modules.sku.entity.PmsSkuImages;
import weiy.top.modules.sku.entity.PmsSkuInfo;
import weiy.top.modules.sku.entity.PmsSkuSaleAttrValue;
import weiy.top.modules.sku.service.IPmsSkuImagesService;
import weiy.top.modules.sku.service.IPmsSkuInfoService;
import weiy.top.modules.sku.service.IPmsSkuSaleAttrValueService;
import weiy.top.modules.spu.entity.PmsSpuInfo;
import weiy.top.modules.spu.entity.PmsSpuInfoDesc;
import weiy.top.modules.spu.mapper.PmsSpuInfoMapper;
import weiy.top.modules.spu.service.IPmsSpuImagesService;
import weiy.top.modules.spu.service.IPmsSpuInfoDescService;
import weiy.top.modules.spu.service.IPmsSpuInfoService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import weiy.top.to.SpuBoundTo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: pms_spu_info
 * @Author: weihancheng
 * @Date:   2021-11-24
 * @Version: V1.0
 */
@Service
public class PmsSpuInfoServiceImpl extends ServiceImpl<PmsSpuInfoMapper, PmsSpuInfo> implements IPmsSpuInfoService {

    @Autowired
    private IPmsSpuInfoDescService spuInfoDescService;
    @Autowired
    private IPmsSkuInfoService skuInfoService;
    @Autowired
    private IPmsSpuImagesService imagesService;
    @Autowired
    private IPmsProductAttrValueService attrValueService;
    @Autowired
    private CouponFeignService couponFeignService;
    @Autowired
    private IPmsSkuImagesService skuImagesService;
    @Autowired
    private IPmsSkuSaleAttrValueService skuSaleAttrValueService;

    /**
     * 保存商品spu
     * @param vo
     */
    @Transactional
    @Override
    public void saveSpuInfo(SpuSaveVo vo) {
        // 保存spu信息
        PmsSpuInfo spuInfo = new PmsSpuInfo();
        BeanUtils.copyProperties(vo, spuInfo);
        save(spuInfo);
        String spuId = spuInfo.getId();

        // 保存spu的描述图片 （保存到pms_spu_info_desc）
        List<String> decript = vo.getDecript();
        PmsSpuInfoDesc descEntity = new PmsSpuInfoDesc();
        descEntity.setSpuId(spuId);
        descEntity.setDecript(String.join(",", decript));
        spuInfoDescService.save(descEntity);

        // 保存spu的图片集 pms_spu_images
        List<String> images = vo.getImages();
        imagesService.saveImages(spuInfo.getId(), images);

        // 保存spu的规格参数 pms_product_attr_value
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<PmsProductAttrValue> collect = baseAttrs.stream().map(attr -> {
            PmsProductAttrValue valueEntity = new PmsProductAttrValue();
            valueEntity.setAttrId(attr.getAttrId());
            valueEntity.setAttrValue(attr.getAttrValues());
            valueEntity.setQuickShow(attr.getShowDesc());
            valueEntity.setSpuId(spuId);
            return valueEntity;
        }).collect(Collectors.toList());
        attrValueService.saveProductAttr(collect);

        // 保存spu的积分信息；sms库下的sms_spu_bounds
        Bounds bounds = vo.getBounds();
        SpuBoundTo spuBoundTo = new SpuBoundTo();
        BeanUtils.copyProperties(bounds, spuBoundTo);
        spuBoundTo.setSpuId(spuId);
        Result<?> r = couponFeignService.add(spuBoundTo);
        if (!r.getCode().equals(CommonConstant.SC_OK_200)) {
            log.error("远程保存spu积分信息失败");
        }

        // 保存spu所有对应的sku信息
        List<Skus> skus = vo.getSkus();
        if (skus != null && skus.size() > 0) {
            skus.forEach(item -> {
                String defaultImg = "";
                for (Images image : item.getImages()) {
                    if (image.getDefaultImg() == 1) {
                        defaultImg = image.getImgUrl();
                    }
                }

                PmsSkuInfo skuInfoEntity = new PmsSkuInfo();
                BeanUtils.copyProperties(item, skuInfoEntity);

                skuInfoEntity.setBrandId(spuInfo.getBrandId());
                skuInfoEntity.setCatalogId(spuInfo.getCatalogId());
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setSpuId(spuId);
                skuInfoEntity.setSkuDefaultImg(defaultImg);

                skuInfoService.save(skuInfoEntity);

                String skuId = skuInfoEntity.getSkuId();

                // 保存 sku的图片信息；pms_sku_image
                List<PmsSkuImages> imagesEntities = item.getImages().stream().map(img -> {
                    PmsSkuImages skuImagesEntity = new PmsSkuImages();
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    skuImagesEntity.setDefaultImg(img.getDefaultImg());
                    return skuImagesEntity;
                }).filter(entity -> !StringUtils.isEmpty(entity.getImgUrl()))
                .collect(Collectors.toList());
                skuImagesService.saveBatch(imagesEntities);

                // sku的销售属性信息：pms_sku_sale_attr_value
                List<PmsAttr> attr = item.getAttr();
                List<PmsSkuSaleAttrValue> skuSaleAttrValueEntities = attr.stream().map(a -> {
                    PmsSkuSaleAttrValue attrValueEntity = new PmsSkuSaleAttrValue();
                    BeanUtils.copyProperties(a, attrValueEntity);
                    attrValueEntity.setSkuId(skuId);
                    return attrValueEntity;
                }).collect(Collectors.toList());

                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);

                // sku的优惠、满减等信息；sms库 -> sms_sku_ladder\sms_sku_full_reduction\sms_member_price

                // todo
//                SkuReductionTo skuReductionTo = new SkuReductionTo();
//                BeanUtils.copyProperties(item,skuReductionTo);
//                skuReductionTo.setSkuId(skuId);
//                if(skuReductionTo.getFullCount() >0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1){
//                    R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
//                    if(r1.getCode() != 0){
//                        log.error("远程保存sku优惠信息失败");
//                    }
//                }

            });
        }
    }

    /**
     * 商品上架
     * @param spuId
     */
    @Override
    public void up(String spuId) {
        // 查出当前spuId对应的所有sku信息,品牌的名字
        List<PmsSkuInfo> skuInfoEntities = skuInfoService.getSkusBySpuId(spuId);

        // 查出当前sku的所有可以被用来检索的规格属性
        List<PmsProductAttrValue> productAttrValueEntities = attrValueService.listBySpuId(spuId);
        List<String> attrIds = productAttrValueEntities.stream().map(PmsProductAttrValue::getAttrId).collect(Collectors.toList());

        List<String> searchIds = attrValueService.selectSearchAttrIds(attrIds);

    }
}
